package com.cy.law.login.service.impl;

import com.cy.law.db.dao.HumanDao;
import com.cy.law.db.entity.Human;
import com.cy.law.login.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Transactional(rollbackFor = Exception.class)
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    @Autowired
    HumanDao humanDao;

    @Override
    public Human validateLogin(String name, String pwd) {
        try {
            if (StringUtils.isEmpty(pwd)) {
                return humanDao.findFirstByHumanName(name);
            } else {
                Human human = humanDao.findFirstByHumanNameAndPassword(name, pwd);
                if (human == null) {
                    human = humanDao.findFirstByHumanCodeAndPassword(name, pwd);
                }
                return human;
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }
}
